package com.chahua.organized.common;

import lombok.Data;

/**
 * @author chahua
 * @date 2022/4/9 18:36
 **/
@Data
public class DataResult<T> {

    /**
     * 响应码
     */
    private int code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    public DataResult(int code,T data){
        this.code = code;
        this.data = data;
    }

    public DataResult(int code,String message){
        this.code = code;
        this.message = message;
    }

    public DataResult(int code,String message,T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public DataResult(BaseResponseCode baseResponsreCode, T data){
        this.code = baseResponsreCode.getCode();
        this.message = baseResponsreCode.getMessage();
        this.data = data;
    }

    public DataResult(){
        this.code = BaseResponseCode.SUCCESS.getCode();
        this.message = BaseResponseCode.SUCCESS.getMessage();
        this.data = null;
    }

    public DataResult(T data){
        this.code = BaseResponseCode.SUCCESS.getCode();
        this.message = BaseResponseCode.SUCCESS.getMessage();
        this.data = data;
    }

    /**
     * 操作成功 data为null
     */
    public static DataResult success(){
        return new DataResult();
    }

    /**
     * 操作成功 data 不为null
     */
    public static <T>DataResult succuess(T data){
        return new DataResult(data);
    }

    /**
     * 自定义 返回操作 data 可控
     */
    public static <T>DataResult getResult(int code,String msg,T data){
        return new DataResult(code,msg,data);
    }

    /**
     * 自定义返回 data为null
     */
    public static DataResult getResult(int code,String msg){
        return new DataResult(code,msg);
    }

    /**
     * 自定义返回 入参一般是异常code枚举 data为空
     */
    public static DataResult getResult(BaseResponseCode responseCode){
        return new DataResult(responseCode);
    }

    /**
     * 自定义返回 入参一般是异常code枚举 data 可控
     */
    public static <T>DataResult getResult(BaseResponseCode responseCode, T data){
        return new DataResult(responseCode,data);
    }
}
